package com.jeason.blogsearch.elasticsearch.controller;

import com.jeason.blogcommon.domain.AjaxResult;
import io.swagger.annotations.Api;
import com.jeason.blogsearch.elasticsearch.service.ElasticService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author 王欢
 * @date 2020/11/3 17:32
 */
@Api(value = "Elastic接口", tags = {"Elastic测试接口"})
@RestController
@RequestMapping("/elastic")
@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE, RequestMethod.PUT})
@Slf4j
public class ElasticController {
    @Autowired
    ElasticService elasticService;

    @ApiOperation(value = "创建索引库和映射关系", notes = "创建索引库和映射关系")
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public AjaxResult createIndex(@RequestParam String indexName) {
        return AjaxResult.success(elasticService.createIndex(indexName));
    }

    @ApiOperation(value = "是否存在索引接口", notes = "是否存在索引接口")
    @RequestMapping(value = "/index/exists", method = RequestMethod.GET)
    public AjaxResult indexExists(@RequestParam String indexName) {
        return AjaxResult.success(elasticService.indexExists(indexName));
    }

    @ApiOperation(value = "删除索引接口", notes = "删除索引接口")
    @RequestMapping(value = "/index", method = RequestMethod.DELETE)
    public AjaxResult deleteIndex(@RequestParam String indexName) {
        return AjaxResult.success(elasticService.deleteIndex(indexName));
    }


    @ApiOperation(value = "普通查询接口", notes = "普通查询接口")
    @RequestMapping(value = "/index/query", method = RequestMethod.GET)
    public AjaxResult query(@RequestParam String keyword) throws IOException {
        return AjaxResult.success(elasticService.query(keyword));
    }

    @ApiOperation(value = "批量插入数据", notes = "批量插入数据")
    @RequestMapping(value = "/index/insertBatch", method = RequestMethod.GET)
    public AjaxResult insertBatch(@RequestParam String indexName) {
        elasticService.insertBatch(indexName);
        return AjaxResult.success();
    }

}
